package service

import (
	"errors"
	"im/model"
	// "log"
	"time"
)

type ContactService struct {
}

// 添加好友
func (service *ContactService) AddFriend(
	userid,
	dstid int64) error {
	// 如果加自己
	if userid == dstid {
		return errors.New("不能添加自己为好友啊")
	}
    // 查询要添加的用户存在
    user := model.User{}
	DbEng.Table(new(model.User)).Where("id=?", dstid).
		Get(&user)
    // log.Println(user) 
	if user.Id <= 0 {
		return errors.New("该用户不存在")
	}
	// 查询是否已经是好友
	tmp := model.Contact{}
	DbEng.Where("owner_id=?", userid).
		And("dst_id=?", dstid).
		And("cate=?", model.CONTACT_CATE_USER).
		Get(&tmp)
	if tmp.Id > 0 {
		return errors.New("该用户已经被添加过啦")
	}
	// 事务
	session := DbEng.NewSession()
	session.Begin()
	// 插入自己的好友记录
	_, e2 := session.InsertOne(model.Contact{
		OwnerId:  userid,
		DstId:   dstid,
		Cate:     model.CONTACT_CATE_USER,
		CreateTime: time.Now(),
	})
	// 好友插入自己的记录
	_, e3 := session.InsertOne(model.Contact{
		OwnerId:  dstid,
		DstId:   userid,
		Cate:     model.CONTACT_CATE_USER,
		CreateTime: time.Now(),
	})
	if e2 == nil && e3 == nil {
		session.Commit()
		return nil
	} else {
		session.Rollback()
		if e2 != nil {
			return e2
		} else {
			return e3
		}
	}
}

// 查找好友
func (service *ContactService) SearchFriend(userId int64) []model.User {
    // 存储联系人
	contacts := make([]model.Contact, 0)
    // 好友id集合
	friendIds := make([]int64, 0)
	DbEng.Where("owner_id=? and cate=?", userId, model.CONTACT_CATE_USER).Find(&contacts)
    // log.Println(contacts) 
	for _, v := range contacts {
		friendIds = append(friendIds, v.DstId)
	}
    // log.Println(friendIds)
	friends := make([]model.User, 0)
	if len(friendIds) == 0 {
		return friends
	}
	DbEng.In("id", friendIds).Find(&friends)
	return friends
}

// 查找群
func (service *ContactService) SearchComunity(userId int64) []model.Community {
	conconts := make([]model.Contact, 0)
	communityIds := make([]int64, 0)

	DbEng.Where("OwnerId = ? and cate = ?", userId, model.CONTACT_CATE_COMMUNITY).Find(&conconts)
	for _, v := range conconts {
		communityIds = append(communityIds, v.DstId)
	}
	communitys := make([]model.Community, 0)
	if len(communityIds) == 0 {
		return communitys
	}
	DbEng.In("id", communityIds).Find(&communitys)
	return communitys
}

// 加群
func (service *ContactService) JoinCommunity(userId, communityId int64) error {
	contact := model.Contact{
		OwnerId: userId,
		DstId:  communityId,
		Cate:    model.CONTACT_CATE_COMMUNITY,
	}
	DbEng.Get(&contact)
	if contact.Id == 0 {
		contact.CreateTime = time.Now()
		_, err := DbEng.InsertOne(contact)
		return err
	} else {
		return nil
	}
}

// 建群
func (service *ContactService) CreateCommunity(community model.Community) (ret model.Community, err error) {
	if len(community.Name) == 0 {
		err = errors.New("缺少群名称")
		return ret, err
	}
	if community.OwnerId == 0 {
		err = errors.New("请先登录")
		return ret, err
	}
	com := model.Community{
		OwnerId: community.OwnerId,
	}
	num, err := DbEng.Count(&com)
	if num > 5 {
		err = errors.New("一个用户最多只能创建5个群")
		return com, err
	} else {
		com.CreateTime = time.Now()
		session := DbEng.NewSession()
		session.Begin()
		_, err = session.InsertOne(&com)
		if err != nil {
			session.Rollback()
			return com, err
		}
		_, err = session.InsertOne(
			model.Contact{
				OwnerId:  com.OwnerId,
				DstId:   com.OwnerId,
				Cate:     model.CONTACT_CATE_COMMUNITY,
				CreateTime: time.Now(),
			})
		if err != nil {
			session.Rollback()
		} else {
			session.Commit()
		}
		return com, err
	}
}

